import { BizKfExtraProColumnType } from '@/components';
import * as API from '@/services';
import { Divider, message, Space, Typography } from 'antd';
import {KfModal, KfPagingTablePage, KfPagingTablePagePropsAction, useBackendEnum, useKeepAliveContext} from 'kf-web-library';
import React, { useRef } from 'react';
import { useLocation, useAccess, useParams, useRequest } from 'umi';
import QuickForm from './components/QuickForm';
import './index.less';
import defaultPlaceHolderImg from '@/assets/default_placeholder.png';
import { CheckCircleOutlined, EditOutlined, RocketOutlined } from '@ant-design/icons';
import { KeepAliveContext } from '@umijs/max';
import { KeepAliveContextProps } from '@@/plugin-keepalive/context';

type Entity = API.JournalPeriod;
type EntityPageQueryParams = API.RolePageQueryParams;
const entityTypeName = '刊期';
const EntityQuickForm = QuickForm;

interface EntityTableListProps {}

/**
 * 刊期 列表页面
 */
const EntityTableList: React.FC<EntityTableListProps> = (props) => {
  const location = useLocation();
  const { journalId } = useParams();
  const access = useAccess();
  const { enumDict, enumTypeDict, getBackendEnum } = useBackendEnum();
  const actionRef = useRef<KfPagingTablePagePropsAction<Entity, EntityPageQueryParams>>();
  const { updateTab } = useKeepAliveContext() || {};

  const journalDetail = useRequest(() => API.queryJournal(journalId!), {
    refreshDeps: [journalId],
    ready: !!journalId,
    onSuccess: (res) => {
      updateTab?.(location.pathname, {
        name: `《${res?.journalNameCn}》的刊期`
      });
    },
  });

  return (
    <KfPagingTablePage<Entity, EntityPageQueryParams, BizKfExtraProColumnType>
      actionRef={actionRef}
      entityTypeName={entityTypeName}
      pagingQuery={(p) => {
        return API.queryJournalPeriods({ journalId, ...p });
      }}
      opActionsDef={{
        single: {
          deleteOne: (record) => {
            KfModal.deleteConfirm(record.id as string, () => {
              API.deleteJournalPeriod(record.id!).then((res) => {
                if (res.flag) {
                  message.info('删除成功');
                  actionRef.current?.reload();
                }
              });
            });
          },
        },
        multi: {
          deleteMultiple: (records) => {
            KfModal.batchActionConfirm('删除', records.length, () => {
              API.deleteJournalPeriods(records.map((r) => r.id!)).then((res) => {
                if (res.flag) {
                  message.info('批量删除成功');
                  actionRef.current?.reload();
                }
              });
            });
          },
        },
      }}
      opFloatingsDef={{
        noargs: {
          createModal: () => ({
            title: `新建`,
            content: <EntityQuickForm formType={'create'} />,
          }),
        },
        single: {
          modifyModal: (record) => ({
            title: `编辑`,
            content: <EntityQuickForm formType={'modify'} bizId={record.id} />,
          }),
          viewModal: (record) => ({
            fltType: 'modal',
            title: `查看`,
            content: <EntityQuickForm formType={'view'} bizId={record.id} />,
          }),
        },
      }}
      proTable={{
        tableBarActions: (action, rows) => {
          return [];
        },
        search: {
          optionRender: (searchConfig, formProps, dom) => [...dom],
        },
      }}
      columns={[
        {
          title: '关键字',
          dataIndex: 'searchText',
          sorter: false,
          hideInTable: true,
          hideInForm: true,
          fieldProps: {
            placeholder: '名称和编码',
          },
        },
        {
          title: '序号',
          width: 50,
          key: '_seq_index_',
          align: 'center',
          hideInSearch: true,
          valueType: 'index',
          render: (dom, record, index) => {
            const pageInfo = actionRef.current?.queryParams();
            return ((pageInfo?.current || 1) - 1) * (pageInfo?.pageSize || 0) + (index + 1);
          },
        },

        {
          title: '封面',
          dataIndex: nameof<Entity>((x) => x.journalCoverPath),
          width: 120,
          sorter: false,
          hideInSearch: true,
          ellipsis: true,
          align: 'center',
          valueType: 'image',
          fieldProps: {
            width: 75,
            height: 106,
            fallback: defaultPlaceHolderImg,
          },
          renderText: (text, record) => {
            return record.journalCoverPath || record.journal?.journalCoverPathUrl

          }
        },
        {
          title: '刊期',
          dataIndex: nameof<Entity>((x) => x.periodName),
          sorter: false,
          hideInSearch: true,

          render: (text, record) => {
            return <div>
              <div>{record.journal?.journalNameCn}</div>
              <div>{record.periodName}</div>
            </div>
          }
        },
        {
          title: '文章数',
          dataIndex: nameof<Entity>((x) => x.articleTotal),
          width: 100,
          align: 'center',
          sorter: false,
          hideInSearch: true,
        },
        {
          title: '创建/更新',
          dataIndex: nameof<Entity>((x) => x.createTime),
          width: 200,
          align: 'center',
          sorter: false,
          hideInSearch: true,
          ellipsis: true,
          render: (_, record) => {
            return (
              <Space direction={'vertical'}>
                <div>
                  <RocketOutlined /> {record.createTime}
                </div>
                <div>
                  <EditOutlined /> {record.modifyTime}
                </div>
              </Space>
            );
          },
        },
        {
          title: '操作',
          fixed: 'right',
          width: 130,
          dataIndex: 'option',
          valueType: 'option',
          render: (_, record) => (
            <Space size={0} split={<Divider type="vertical" />}>
              {/*<a
                onClick={() => {
                }}
              >
                查看
              </a>*/}
              <a
                onClick={() => {
                  actionRef.current?.openFloating.single?.modifyModal(record, 'modal')
                }}
              >
                编辑
              </a>
              {/*<a onClick={() => actionRef.current?.executeAction.single?.deleteOne(record)}>
                <Typography.Text type="danger">删除</Typography.Text>
              </a>*/}
            </Space>
          ),
        },
      ]}
    />
  );
};

export default EntityTableList;
